package org.shirdrn.document.preprocessing.core.component;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.shirdrn.document.preprocessing.api.Context;
import org.shirdrn.document.preprocessing.core.common.AbstractComponent;

public class BasicInformationCollector extends AbstractComponent {
	
	private static final Log LOG = LogFactory.getLog(BasicInformationCollector.class);
	
	
	public BasicInformationCollector(final Context context) {
		super(context);
	}

	public void fire() {
		// get total document count for computing TF-IDF
		int totalDocCount = 0;
		String labels [] = context.getFDMetadata().getInputRootDir().list(new FilenameFilter(){
			//edited by alex 
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				if(name.startsWith("."))
					return false;
				else return true;
			}
			
		});
		for(String label : labels) {
			context.getVectorMetadata().addLabel(label);
			LOG.info("Add label: label=" + label);
			//step 4
			sendMessage("Add label: label=" + label);
			File labelDir = new File(context.getFDMetadata().getInputRootDir(), label);
			File[] files = labelDir.listFiles(new FileFilter() {
				public boolean accept(File pathname) {
					return pathname.getAbsolutePath().endsWith(context.getFDMetadata().getFileExtensionName());
				}
			});
			context.getVectorMetadata().putLabelledTotalDocCount(label, files.length);
			//step 4
			LOG.info("Put document count: label= " + label + ", docCount=" + files.length);
			sendMessage("Put document count: label= " + label + ", docCount=" + files.length);
			totalDocCount += files.length;
		}

		LOG.info("Total documents: totalCount= " + totalDocCount);
		//step4
		sendMessage("Total documents: totalCount= " + totalDocCount);
		context.getVectorMetadata().setTotalDocCount(totalDocCount);
	}

}
